//
// TcpServerDispatcher.h
//
// $Id: //poco/1.4/Net/include/Poco/Net/TcpServerDispatcher.h#1 $
//
// Library: Net
// Package: TcpServer
// Module:  TcpServerDispatcher
//
// Definition of the TcpServerDispatcher class.
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#ifndef Net_TcpServerDispatcher_INCLUDED
#define Net_TcpServerDispatcher_INCLUDED


#include "Poco/Net/Net.h"
#include "Poco/Net/TcpServerConnectionFactory.h"
#include "Poco/Net/TcpServerParams.h"
#include "Poco/Runnable.h"
#include "Poco/NotificationQueue.h"
#include "Poco/ThreadPool.h"
#include "Poco/Mutex.h"


namespace Poco {
namespace Net {


class Net_API TcpServerDispatcher: public Poco::Runnable
	/// A helper class for TcpServer that dispatches
	/// connections to server connection threads.
{
public:
	TcpServerDispatcher(TcpServerConnectionFactory* pFactory, Poco::ThreadPool& threadPool, TcpServerParams* pParams);
		/// Creates the TcpServerDispatcher.
		///
		/// The dispatcher takes ownership of the TcpServerParams object.
		/// If no TcpServerParams object is supplied, the TcpServerDispatcher
		/// creates one.

	void duplicate();
		/// Increments the object's reference count.

	void release();
		/// Decrements the object's reference count
		/// and deletes the object if the count
		/// reaches zero.	

	void run();
		/// Runs the dispatcher.
		
	void enqueue(int fd);
		/// Queues the given socket connection.

	void stop();
		/// Stops the dispatcher.
			
	int currentThreads() const;
		/// Returns the number of currently used threads.

	int maxThreads() const;
		/// Returns the maximum number of threads available.
		
	int totalConnections() const;
		/// Returns the total number of handled connections.
		
	int currentConnections() const;
		/// Returns the number of currently handled connections.	

	int maxConcurrentConnections() const;
		/// Returns the maximum number of concurrently handled connections.	
		
	int queuedConnections() const;
		/// Returns the number of queued connections.	
	
	int refusedConnections() const;
		/// Returns the number of refused connections.

	const TcpServerParams& params() const;
		/// Returns a const reference to the TcpServerParam object.

protected:
	~TcpServerDispatcher();
		/// Destroys the TcpServerDispatcher.

	void beginConnection();
		/// Updates the performance counters.
		
	void endConnection();
		/// Updates the performance counters.

private:
	TcpServerDispatcher();
	TcpServerDispatcher(const TcpServerDispatcher&);
	TcpServerDispatcher& operator = (const TcpServerDispatcher&);

	int 							m_rc;
	TcpServerParams* 				m_pParams;
	int  							m_currentThreads;
	int  							m_totalConnections;
	int  							m_currentConnections;
	int  							m_maxConcurrentConnections;
	int  							m_refusedConnections;
	bool 							m_stopped;
	Poco::NotificationQueue         m_queue;
	TcpServerConnectionFactory* 	m_pConnectionFactory;
	Poco::ThreadPool&               m_threadPool;
	mutable Poco::FastMutex         m_mutex;
};


//
// inlines
//
inline const TcpServerParams& TcpServerDispatcher::params() const
{
	return *m_pParams;
}


} } // namespace Poco::Net


#endif // Net_TcpServerDispatcher_INCLUDED
